package com.weetop.aklyscan.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseFragment;
import com.weetop.aklyscan.base.mvp.BasePresenter;
import com.weetop.aklyscan.utils.Tablayout.SegmentTabLayout;
import com.weetop.aklyscan.utils.Tablayout.listener.OnTabSelectListener;
import com.weetop.aklyscan.utils.widget.MyDialogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class Fragment_zx extends BaseFragment {
    private Fragment[] fragments;
    @BindView(R.id.tl_1)
    SegmentTabLayout tl1;
    Unbinder unbinder;

    private Fragment_zx_op fragment_zx_op;
    private Fragment_zx_instock fragment_zx_instock;
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    private String[] mTitles = {"装箱", "入库"};
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_in_zx, null);
        unbinder = ButterKnife.bind(this, view);
        fragment_zx_op=new Fragment_zx_op();
        fragment_zx_instock=new Fragment_zx_instock();
        fragments = new Fragment[]{fragment_zx_op, fragment_zx_instock};
        tl1.setTabData(mTitles);
        tl1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchfragment(fragments[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        switchfragment(fragment_zx_op);
        return view;
    }
    /**
     * 选择fragment
     *
     * @param fragment
     */
    private void switchfragment(Fragment fragment) {
        // 设置一个默认值
        if (fragment == null) {
            fragment = fragment_zx_op;
        }
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment1 : fragments) {
            if (fragment1 != fragment) {
                if (fragment1 != null && !fragment1.isHidden() && fragment1.isAdded()) {
                    ft.hide(fragment1);
                }
            }
        }

        if (fragment.isAdded() && fragment.isHidden()) {
            ft.show(fragment);
        } else {
            if (!fragment.isAdded()) {
                ft.add(R.id.frame_in_zx_op_tab, fragment);
            }
        }
        ft.commitAllowingStateLoss();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Override
    public void onDataError() {

    }

    @Override
    public void onUpFail(String mag) {
    }
}
